/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        return Depth(root) != -1;
    }
    private int Depth(TreeNode root) {

        if(null == root) {
            return 0;
        }
        int leftdepth = Depth(root.left);
        if(-1 == leftdepth) {
            return -1;
        }
        int rightdepth = Depth(root.right);
        if(-1 == rightdepth) {
            return -1;
        }
        return Math.abs(leftdepth - rightdepth) < 2 ? Math.max(leftdepth,rightdepth) + 1 : -1;
    }
}